<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="35%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="数据类型名称" prop="typeName">
            <el-input v-model="formData.typeName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据类型标识" prop="typeCode">
            <el-input v-model="formData.typeCode" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据接口" prop="interfaceId">
            <el-select
              v-model="formData.interfaceId"
              placeholder="请选择数据接口"
              clearable
              @change="handleChangeInterface"
            >
              <el-option
                v-for="item in interfaceDatas"
                :key="item.id"
                :label="item.interfaceName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="interfaceAddress">
            <el-input
              v-model="interfaceAddress"
              placeholder="请输入"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="端口号" prop="interfacePort">
            <el-input v-model="interfacePort" placeholder="请输入" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据长度" prop="dataLength">
            <el-input
              v-model="formData.dataLength"
              type="number"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据表头标识" prop="startCode">
            <el-input v-model="formData.startCode" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据结束标识" prop="endCode">
            <el-input v-model="formData.endCode" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import {
  addDataType,
  updateDataType,
  listAllAddressPort,
} from "@/api/statistical";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      interfaceDatas: [], //数据接口
      interfaceAddress: "",
      interfacePort: 0,
      formData: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "数据类型名称不能为空", trigger: "blur" },
        ],
        typeCode: [
          { required: true, message: "数据类型标识不能为空", trigger: "blur" },
        ],
        interfaceId: [
          { required: true, message: "请选择数据接口", trigger: "change" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.formData = this.dialogData;
      listAllAddressPort().then((res) => {
        console.log("listAllAddressPort", res);
        if (res.code == 200) {
          this.interfaceDatas = res.data;
          if (this.formData.id) {
            this.interfaceDatas.forEach((item) => {
              if (item.id == this.formData.id) {
                this.interfaceAddress = item.interfaceMulticastAddress;
                this.interfacePort = item.interfacePort;
              }
            });
          }
        }
      });
    },
    handleChangeInterface(val) {
      this.interfaceDatas.forEach((item) => {
        if (item.id == val) {
          this.interfaceAddress = item.interfaceMulticastAddress;
          this.interfacePort = item.interfacePort;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData);
        if (valid) {
          if (this.formData.id != undefined) {
            console.log("update");
            updateDataType(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          } else {
            addDataType(this.formData).then((response) => {
              console.log("add", response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
  },
};
</script>
